package com.example.demo.controller;

import com.example.demo.pojo.PageResult;
import com.example.demo.pojo.Result;
import com.example.demo.pojo.dto.AdminLoginDTO;
import com.example.demo.pojo.dto.AdminPageDTO;
import com.example.demo.pojo.dto.AdminRegisterDTO;
import com.example.demo.pojo.entity.Admin;
import com.example.demo.pojo.vo.AdminLoginVO;
import com.example.demo.service.AdminService;
import com.example.demo.utils.JwtUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/polycystic/admin")
public class AdminController {

    @Autowired
    private AdminService adminService;

    @PostMapping("/login")
    public Result<AdminLoginVO> login(@RequestBody AdminLoginDTO adminLoginDTO){
        Admin admin=adminService.login(adminLoginDTO);

        Map<String,Object> claims=new HashMap<>();
        claims.put("id",admin.getId());
        String jwt= JwtUtils.generateJwt(claims);

        AdminLoginVO adminLoginVO = new AdminLoginVO();
        BeanUtils.copyProperties(admin,adminLoginVO);
        adminLoginVO.setToken(jwt);
        return Result.success(adminLoginVO);
    }

    @PostMapping("/add")
    public Result register(@RequestBody AdminRegisterDTO adminRegisterDTO){
        adminService.register(adminRegisterDTO);
        return Result.success();
    }

    @DeleteMapping("/delete")
    public Result delete(@PathVariable Long id){
        adminService.delete(id);
        return Result.success();
    }

    @PostMapping("/page")
    public Result<PageResult> page(@RequestBody AdminPageDTO adminPageDTO){
        PageResult pageResult=adminService.page(adminPageDTO);
        return Result.success(pageResult);
    }
}
